#pragma once

#include <QtWidgets/QWidget>
#include <QtWidgets/QVBoxLayout>
#include "bondchart.h"
#include "bondcharttools.h"
#include <widgets/chart/basechart_axis.h>
#include <widgets/chart/basechart_layoutitem.h>

namespace qb
{
	class xCDCKlineDayUnit_c
	{
	public:
		float		m_CleanPrice;										//估值净价
		float       m_DealClean;                                        //成交净价（包含收盘和净价，和成交曲线的日均，收盘联动）
		float		m_ValueYield;									    //估值收益率
		float       m_DealYield;                                        //成交收益率（包含收盘和净价，和成交曲线的日均，收盘联动）
		float       m_QuoteBid;                                         //报价收益率bid（目前只在收益率界面有数据）
		float       m_QuoteOfr;                                         //报价收益率ofr（目前只在收益率界面有数据）
		unsigned int		m_Date;												//日期

		bool operator==(const xCDCKlineDayUnit_c& rhs) const
		{
			return (m_Date == rhs.m_Date);
		}
	};

	class xCDCKlineDayList_c {
	public:
		char	m_bond_key[32 + 1];									//Sumscope的BondKey	String	
		char	m_listed_market[8];
		std::list<xCDCKlineDayUnit_c> m_List;						//
		xCDCKlineDayList_c()
		{
			memset(m_bond_key, 0, sizeof(m_bond_key));
			memset(m_listed_market, 0, sizeof(m_listed_market));
		};
	};

    class BondQuoteCDCChart
		: public QWidget
		, public BaseChartCustomAxisGrid
		, public BaseChartCustomTextElement
    {
		Q_OBJECT
    public:
		BondQuoteCDCChart(QWidget* parent = nullptr);
		~BondQuoteCDCChart();

		void setBondInfo(const CBondInfo* bondinfo);
		void setOptionBond(bool bOption);
		void setCheck(int idx, int check);
		void loadKLineData(xCDCKlineDayList_c* pData);
		void createChart();
		void refreshData();
		void setTab(int tab);
		void setMarketType(em_BrokerKinds emCurType);
		void setDateSel(int nIndex);
		void setCalendar(int startDay, int endDay);
		void setLeftDrag(double ratio);
		void setRightDrag(double ratio);
		void setCentralDrag(double ratio1, double ratio2);

		PriceKLineList* GetPriceKLineList();

		BaseChartDragBarElement* dragBar() { return bcde; }

		// BaseChartCustomAxisGrid
		virtual bool drawGridXLines(BaseChartPainter* painter);
		virtual bool drawGridYLines(BaseChartPainter* painter);

		// BaseChartCustomTextElement
		virtual bool customDraw(BaseChartPainter* painter, const QRect& rect);
	signals:
		void mouseDoubleClick(QWidget* );
		void calendarChanged(int, int);

	protected:
		void customDraw(BaseChartPainter* painter, const QRect& rect, int count, QColor* clr);
		void inputKLineData(xCDCKlineDayList_c* pData);
		void GetShowLimit(double fMinXShow, double fMaxXShow);
		int GetKLineIndex(int nIndex, std::list<PriceKLine>::iterator& it);
		int getDateSel(int nIndex);
		bool getCalendarSel(int& nBegin, int& nEnd);
		bool isValidCalendar(int& nStartDay, int& nEndDay);
		void drawMouseMoveLine(BaseChartPainter* painter);
		QString	GetKLineDateString(std::list<PriceKLine>::iterator& it);
		QString	strInt2DateInTable(UINT nDate);
		double getXPos_Value(double value);

		void paintEvent(QPaintEvent* event) override;
		void leaveEvent(QEvent* event) override;

		Q_SLOT void onMouseMove(QMouseEvent* event);

	private:
		BaseChartCustomPlot*		customPlot		= nullptr;
		QVBoxLayout*				verticalLayout	= nullptr;
		BaseChartTextElement*		bcte			= nullptr;
		BaseChartDragBarElement*	bcde			= nullptr;

		CBondInfo*					mBondInfo		= nullptr;
		PriceKLineList				m_DataKLineBroker;//报价，成交日线
		PriceKLineList				m_DataKLineCFETS;
		PriceKLineList				m_DataKLineExchange;

		int							m_currentTab;
		int							m_nMarketType;
		int							m_nHotPoint;
		int							m_nDateSel;
		int							m_nStartDay;
		int							m_nEndDay;
		int							m_nBegin;
		int							m_nEnd;
		bool						m_bOptionBond;//是否为含权债，含权债显示行权和到期收益率；右侧坐标无需显示
		bool						m_bCDCLocked;//CDC权限
		std::vector<int>			m_EvaType;
		double						m_dMinX;
		double						m_dMaxX;
		double						m_dMinY;
		double						m_dMaxY;
		double						m_dOriginMinX;
		double						m_dOriginMaxX;
		double						m_dDragLeft;
		double						m_dDragRight;
    };
}

